/*
 * @Author: TC
 * @Date: 2023-11-11 17:30:26
 * @Last Modified by: TC
 * @Last Modified time: 2025-06-20 17:30:37
 */

import { v2, v3, Vec2, Vec3, view } from "cc";

/**
 * 屏幕相关逻辑
 */
export class ViewLogic {
    /**战斗界面锚点 */
    private static _anchorX = 0.5;
    private static _anchorY = 0.5;
    private static _left = 0;
    private static _right = 0;
    private static _top = 0;
    private static _bottom = 0;

    /**
     * 设置锚点
     * @param anchorX
     * @param anchorY
     */
    public static setAnchor(anchorX: number, anchorY: number): void {
        this._anchorX = anchorX;
        this._anchorY = anchorY;

        let size = view.getVisibleSize();
        this._left = -this._anchorX * size.width;
        this._right = size.width * (1 - this._anchorX);
        this._top = size.height * (1 - this._anchorY);
        this._bottom = -size.height * this._anchorY;
    }

    /**
     * 根据方向计算角度
     * @param x 横坐标
     * @param y 纵坐标
     * @param refer 参考轴角度(项目是竖版游戏，默认向上为0)
     * @returns
     */
    public static getAngleByVec(x: number, y: number, refer: number = 90): number {
        return (Math.atan2(y, x) / Math.PI) * 180 - refer;
    }

    /**
     * 根据角度计算向量
     * @param angle 角度
     * @param refer 参考轴角度(项目是竖版游戏，默认向上为0)
     */
    public static getVecByAngle(angle: number, refer: number = 90): Vec3 {
        let vec: Vec3 = v3();
        vec.x = Math.cos(((angle + refer) / 180) * Math.PI);
        vec.y = Math.sin(((angle + refer) / 180) * Math.PI);
        vec.normalize();
        return vec;
    }

    /**
     * 节点位置超出屏幕
     * @param 坐标
     * @param distance 超出屏幕的距离
     * @returns
     */
    public static offScreen(x: number, y: number, distance: number): boolean {
        if (this.beyondScreenLeft(x, y, distance)) {
            return true;
        }
        if (this.beyondScreenRight(x, y, distance)) {
            return true;
        }
        if (this.beyondScreenBottom(x, y, distance)) {
            return true;
        }
        if (this.beyondScreenTop(x, y, distance)) {
            return true;
        }
        return false;
    }

    /**
     * 坐标超过屏幕左侧
     * @param x
     * @param y
     * @param distance
     * @returns
     */
    public static beyondScreenLeft(x: number, y: number, distance: number = 0): boolean {
        return x < this._left - distance;
    }

    /**
     * 坐标超过屏幕右侧
     * @param x
     * @param y
     * @param distance
     * @returns
     */
    public static beyondScreenRight(x: number, y: number, distance: number = 0): boolean {
        return x > this._right + distance;
    }

    /**
     * 坐标超过屏幕下侧
     * @param x
     * @param y
     * @param distance
     * @returns
     */
    public static beyondScreenBottom(x: number, y: number, distance: number = 0): boolean {
        return y < this._bottom - distance;
    }

    /**
     * 坐标超过屏幕上侧
     * @param x
     * @param y
     * @param distance
     * @returns
     */
    public static beyondScreenTop(x: number, y: number, distance: number = 0): boolean {
        return y > this._top + distance;
    }
}
